<!--
 * @Description: 用户注册组件
 * @Author: hai-27
 * @Date: 2020-02-19 22:20:35
 * @LastEditors: hai-27
 * @LastEditTime: 2020-03-01 15:34:34
 -->
<template>
  <div id="register">
    <el-dialog title="注册" width="300px" center :visible.sync="isRegister">
      <el-form
        :model="RegisterUser"

        status-icon
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="name">
          <el-input
            prefix-icon="el-icon-user-solid"
            placeholder="请输入账号"
            v-model="RegisterUser.name"
          ></el-input>
        </el-form-item>
        <el-form-item prop="pass">
          <el-input
            prefix-icon="el-icon-view"
            type="password"
            placeholder="请输入密码"
            v-model="RegisterUser.pass"
          ></el-input>
        </el-form-item>
        <el-form-item prop="confirmPass">
          <el-input
            prefix-icon="el-icon-view"
            type="password"
            placeholder="请再次输入密码"
            v-model="RegisterUser.confirmPass"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="medium" type="primary" @click="Register" style="width:100%;">注册</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "MyRegister",
  props: ["register"],
  data() {
    // 用户名的校验方法

    return {
      isRegister: false, // 控制注册组件是否显示
      RegisterUser: {
        name: "",
        pass: "",
        confirmPass: ""
      },
    };
  },
  watch: {
    // 监听父组件传过来的register变量，设置this.isRegister的值
    register: function(val) {
      if (val) {
        this.isRegister = val;
      }
    },
    // 监听this.isRegister变量的值，更新父组件register变量的值
    isRegister: function(val) {
      if (!val) {
        this.$refs["ruleForm"].resetFields();
        this.$emit("fromChild", val);
      }
    }
  },
  methods: {
    Register() {
      axios.post('http://myphoenixshop.cpolar.io/register', {
        account: this.RegisterUser.name,
        password: this.RegisterUser.pass,
        username: "test"
      }).then(res => {
        console.log(res.data.msg);
        console.log(res);
        if (res.data.msg == "success") {
          this.$message({
            type: 'success',
            message: '注册成功'
          });
          this.isRegister = false;
        } else {
          this.$message({
            type: 'error',
            message: res.message
          });
        }
      })
      // this.$axios
      //   .post("/api/users/register", {
      //     userName: this.RegisterUser.name,
      //     password: this.RegisterUser.pass
      //   })
      //   .then(res => {
      //     // “001”代表注册成功，其他的均为失败
      //     if (res.data.code === "001") {
      //       // 隐藏注册组件
      //       this.isRegister = false;
      //       // 弹出通知框提示注册成功信息
      //       this.notifySucceed(res.data.msg);
      //     } else {
      //       // 弹出通知框提示注册失败信息
      //       this.notifyError(res.data.msg);
      //     }
      //   })
      //   .catch(err => {
      //     return Promise.reject(err);
      //   });


    }
  }
};
</script>
<style>
</style>
